*Determines DMSPE combination forecasts

set more 1


do prepare

if $freq==4{
	gen double theta=$theta^40 if yy==19551
	replace theta=theta[_n-1]/$theta if yy>19551
}

if $freq==12{
	gen double theta=$theta^120 if yy==195501
	replace theta=theta[_n-1]/$theta if yy>195501
}

gen double phitotal=0
gen double sphitotal=0
gen double wphitotal=0
gen double wsphitotal=0


*Produce individual out-of-sample forecasts
foreach var of varlist dp-lastpredictor{
	di `var'
	qui{
	gen fxx`var'=.
	gen sfxx`var'=.
	gen double thetafxx`var'=0
	gen double phifxx`var'=0
	gen dfxx`var'=.
	gen double sthetafxx`var'=0
	gen double sphifxx`var'=0
	gen sdfxx`var'=.
	
	gen wfxx`var'=.
	gen wsfxx`var'=.
	gen double wthetafxx`var'=0
	gen double wphifxx`var'=0
	gen wdfxx`var'=.
	gen double wsthetafxx`var'=0
	gen double wsphifxx`var'=0
	gen wsdfxx`var'=.

	capture drop weight
	gen weight=.

	
	
	forvalues i=$tsamplestart/$tlastpred{
	
		
		
		capture drop rvpred
		capture reg svar l.svar l.svar11 if yy>$samplestart & time<`i' 
		capture predict rvpred 
		capture replace weight = 1/rvpred
	
	
		*with log returns
		capture drop x
		capture reg lnexret l.`var' if yy>$samplestart & time<`i'+1-$h
		capture predict x
		capture replace fxx`var'=x if time==`i'
		
		capture replace thetafxx`var'=thetafxx`var'[_n-1]+(x-lnexret)^2*theta if time==`i' & theta!=.
		capture replace phifxx`var'=1/(thetafxx`var'/theta[`i']) if time==`i' & theta!=.
		capture replace phitotal=phitotal+phifxx`var' if time==`i'
		capture replace dfxx`var'=x*phifxx`var'[_n-$h] if time==`i'
		
		*with simple returns
		capture drop x
		capture reg exret l.`var' if yy>$samplestart & time<`i'+1-$h
		capture predict x
		capture replace sfxx`var'=x if time==`i'
		
		capture replace sthetafxx`var'=sthetafxx`var'[_n-1]+(x-exret)^2*theta if time==`i' & theta!=.
		capture replace sphifxx`var'=1/(sthetafxx`var'/theta[`i']) if time==`i' & theta!=.
		capture replace sphitotal=sphitotal+sphifxx`var' if time==`i'
		capture replace sdfxx`var'=x*sphifxx`var'[_n-$h] if time==`i'
		
		
		*weighted least squares
		
		capture drop x
		capture reg lnexret l.`var' if yy>$samplestart & time<`i'+1-$h  [aweight=weight]
		capture predict x
		capture replace wfxx`var'=x if time==`i'
		
		
		
		capture replace wthetafxx`var'=wthetafxx`var'[_n-1]+(wfxx`var'-lnexret)^2*theta if time==`i' & theta!=.
		capture replace wphifxx`var'=1/(wthetafxx`var'/theta[`i']) if time==`i' & theta!=.
		capture replace wphitotal=wphitotal+wphifxx`var' if time==`i'
		capture replace wdfxx`var'=wfxx`var'*wphifxx`var'[_n-$h] if time==`i'
		
		
		*with simple returns
		
		capture drop x
		capture reg exret l.`var' if yy>$samplestart & time<`i'+1-$h  [aweight=weight]
		capture predict x
		capture replace wsfxx`var'=x if time==`i'
		
		
		
		capture replace wsthetafxx`var'=wsthetafxx`var'[_n-1]+(wsfxx`var'-exret)^2*theta if time==`i' & theta!=.
		capture replace wsphifxx`var'=1/(wsthetafxx`var'/theta[`i']) if time==`i' & theta!=.
		capture replace wsphitotal=wsphitotal+wsphifxx`var' if time==`i'
		capture replace wsdfxx`var'=wsfxx`var'*wsphifxx`var'[_n-$h] if time==`i'
	
		
	
		}
		
		
		
	}
}

*Generate mean forecasts
egen pred=rowtotal(dfxx*)
replace pred=pred/phitotal[_n-$h]


capture drop zpred
gen zpred=pred


egen spred=rowtotal(sdfxx*)
replace spred=spred/sphitotal[_n-$h]



do evaluation


gen what="RSZ dmspe$theta" in 1
local s="ResRSZdmspe$theta$h$sep$freq"
local ds="DataRSZdmspe$theta$h$sep$freq"



export excel what oos* cduoptsmodelmean* cduoptbuyhold*  using results.xlsx in 1/30, sheetmod sheet("`s'") cell(A1) first(var)
export excel yy* pred lnexret modelmean using results.xlsx if yyy>=$firstpred, sheetmod sheet("`s'") cell(A10) first(var)


*with weighted LS
capture drop pred spred
egen pred=rowtotal(wdfxx*)
replace pred=pred/wphitotal[_n-$h]

egen spred=rowtotal(wsdfxx*)
replace spred=spred/wsphitotal[_n-$h]

do evaluation


capture drop what
gen what="wRSZ dmspe$theta" in 1
local s="wResRSZdmspe$theta$h$sep$freq"

export excel what oos* cduoptsmodelmean* cduoptbuyhold*  using results.xlsx in 1/30, sheetmod sheet("`s'") cell(A1) first(var)


